//
//  XGBottomTapView.swift
//  TodayNews
//
//  Created by monkey on 2020/2/12.
//  Copyright © 2020 itcast. All rights reserved.
//

import UIKit

protocol XGBottomTapViewDelegate: AnyObject
{
    func bottomTapClick(button: UIButton, bottomTap: XGBottomTab) -> Void
}

class XGBottomTapView: UIView
{
    /// 代理
    weak var delegate: XGBottomTapViewDelegate?
    
    init(bottomTaps: [XGBottomTab])
    {
        self.bottomTaps = bottomTaps
        
        super.init(frame: CGRect.zero)
        
        setUpUI()
    }
    
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    /// 底部tab模型
    private let bottomTaps: [XGBottomTab]
}

// MARK: - 私有扩展

private extension XGBottomTapView
{
    @objc func bottomTapButtonAction(button: UIButton) -> Void
    {
        delegate?.bottomTapClick(button: button, bottomTap: bottomTaps[button.tag])
    }
    
    func setUpUI() -> Void
    {
        var buttons = [UIButton]()
        for (index,bottomTap) in bottomTaps.enumerated() {
            let button = UIButton(title: bottomTap.name, fontSize: 17, normalColor: UIColor.darkGray, imageName: "tabbar-options_9x9_", target: self, action: #selector(bottomTapButtonAction(button:)))
            button.layoutButtonWithEdgInsetStyle(.imageLeft, space: 10)
            button.tag = index
            buttons.append(button)
        }
        
        let stackView = UIStackView(arrangedSubviews: buttons)
        stackView.distribution = .fillEqually
        addSubview(stackView)
        stackView.snp.makeConstraints { (make) in
            make.edges.equalToSuperview()
        }
        
        let separatorView = UIView()
        separatorView.backgroundColor = UIColor.lightGray
        addSubview(separatorView)
        separatorView.snp.makeConstraints { (make) in
            make.top.left.right.equalToSuperview()
            make.height.equalTo(0.5)
        }
    }
}
